using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Steamworks;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.UI;

namespace Genuineverification
{
    public class Genuineverification : Mod
    {

    }
    public class WorsePlayer1 : ModPlayer
    {
        public int _kickTimer = -1;
        public bool HasSteam;
        public override void OnEnterWorld()
        {
            HasSteam = SteamAPI.Init();
            if (!HasSteam)
            {
                ModContent.GetInstance<GenuineCheckUISystem>().ShowWarningUI(new GenuineWarningUI());
                _kickTimer = 300;
            }
        }
        private void LockControls()
        {
            Player.controlLeft = false;
            Player.controlRight = false;
            Player.controlJump = false;
            Player.controlUseItem = false;
        }
        public override void PostUpdate()
        {
            if (_kickTimer > 0)
            {
                _kickTimer--;
                LockControls();
                if (_kickTimer <= 0)
                {
                    Main.menuMode = 0;
                    Main.gameMenu = true;
                }
            }
        }
    }
    public class GenuineWarningUI : UIState
    {
        private static WorsePlayer1 GetPlayer() => Main.LocalPlayer.GetModPlayer<WorsePlayer1>();
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (Main.LocalPlayer?.active != true || GetPlayer()?._kickTimer <= 0)
            {
                ModContent.GetInstance<GenuineCheckUISystem>().HideWarningUI();
            }
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            var player = GetPlayer();
            if (player == null || player._kickTimer <= 0) return;
            float progress = 1f - (player._kickTimer / 300f);
            string warningText = Language.GetTextValue("Mods.Genuineverification.UI.1");
            string countdownText = $"{progress * 5f:0.0}" + Language.GetTextValue("Mods.Genuineverification.UI.2");
            Vector2 warningPos = new(Main.screenWidth / 2, 100);
            Vector2 countdownPos = new(Main.screenWidth / 2, 140);
            Utils.DrawBorderStringBig(spriteBatch, warningText, warningPos, Color.White, 1.2f, 0.5f, 0.5f);
            Utils.DrawBorderString(spriteBatch, countdownText, countdownPos, Color.White, 1f, 0.5f, 0.5f);
            int barWidth = 400;
            int barHeight = 8;
            int barY = 180;
            int barX = Main.screenWidth / 2 - barWidth / 2;
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle(barX, barY, barWidth, barHeight), new Color(50, 50, 50, 200));
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle(barX, barY, (int)(barWidth * progress), barHeight), Color.Red);
            spriteBatch.Draw(TextureAssets.MagicPixel.Value, new Rectangle(barX, barY, (int)(barWidth * progress), 1), Color.White * 0.3f);
        }
    }
    public class GenuineCheckUISystem : ModSystem
    {
        private UserInterface _genuineCheckInterface;

        public override void Load()
        {
            _genuineCheckInterface = new UserInterface();
        }

        public override void UpdateUI(GameTime gameTime)
        {
            _genuineCheckInterface?.Update(gameTime);
        }

        public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
        {
            int mouseTextIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Mouse Text"));
            if (mouseTextIndex != -1)
            {
                layers.Insert(mouseTextIndex, new LegacyGameInterfaceLayer(
                    "WorseGame: Genuine Check",
                    delegate
                    {
                        _genuineCheckInterface.Draw(Main.spriteBatch, new GameTime());
                        return true;
                    },
                    InterfaceScaleType.UI
                ));
            }
        }

        public void ShowWarningUI(GenuineWarningUI ui)
        {
            _genuineCheckInterface?.SetState(ui);
        }

        public void HideWarningUI()
        {
            _genuineCheckInterface?.SetState(null);
        }
    }
}
